package local.blueheart.data.springdatademo.util;

import local.blueheart.data.springdatademo.domain.config.ApplicationDO;
import local.blueheart.data.springdatademo.domain.config.ArchiveSourceDO;
import local.blueheart.data.springdatademo.domain.config.ItemDO;
import local.blueheart.data.springdatademo.domain.config.ModuleDO;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;

public class Dom4jUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(Dom4jUtil.class);

    public static final String ARCHIVE_SOURCE_EXPRESSION = "//ArchiveSource";

    public static final String ARCHIVE_SOURCE_VERSION_EXPRESSION = "//ArchiveSource/@version";



    public static Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }

    /**
     * 返回配置文件的版本
     * @param document
     * @return
     */
    public static String getArchiveSourceVersion(Document document){
        Node node = document.selectSingleNode(ARCHIVE_SOURCE_VERSION_EXPRESSION);
        return node.getStringValue();
    }

    /**
     * 返回一个ARCHIVE_SOURCE_EXPRESSION节点的Entity
     * @param node
     * @return ArchiveSourceDO
     */
    public static ArchiveSourceDO getArchiveSourceDO(Node node){
        Node aNode = node;
        String type = aNode.valueOf("@type");
        String project = aNode.valueOf("@project");
        String version = aNode.valueOf("@version");
        ArchiveSourceDO archiveSourceDO = new ArchiveSourceDO();
        archiveSourceDO.setType(type);
        archiveSourceDO.setProject(project);
        archiveSourceDO.setConfigVersion(version);
        return archiveSourceDO;
    }

    /**
     * 返回一个ApplicationDO的Entity
     * @param node
     * @return ApplicationDO
     */
    public static ApplicationDO getApplicationDO(Node node){
        //name="目录省公司签报" appField="" flagField="" contentFilePath="" indexPath="lib/qbarchiveindex.nsf" address="10.176.237.79:60148" userName="admin" passWord="aaa123"
        String name = node.valueOf("@name");
        String flagField=node.valueOf("@flagField");
        String contentFilePath = node.valueOf("@contentFilePath");
        String indexPath = node.valueOf("@indexPath");
        String address = node.valueOf("@address");
        String userName = node.valueOf("@userName");
        String passWord = node.valueOf("@passWord");
        ApplicationDO applicationDO = new ApplicationDO();
        applicationDO.setName(name);
        applicationDO.setFlagField(flagField);
        applicationDO.setContentFilePath(contentFilePath);
        applicationDO.setIndexPath(indexPath);
        applicationDO.setAddress(address);
        applicationDO.setUserName(userName);
        applicationDO.setPassWord(passWord);
        return applicationDO;
    }

    /**
     * 返回一个模块的ModuleDO Entity
     * @param node
     * @return ModuleDO
     */
    public static ModuleDO getModuleDO(Node node){
        //name="统一模块" alias="统一模块"
        String name = node.valueOf("@name");
        String alias = node.valueOf("@alias");
        String search = node.valueOf("@search");
        ModuleDO moduleDO = new ModuleDO();
        moduleDO.setName(name);
        moduleDO.setAlias(alias);
        moduleDO.setSearch(search);
        return moduleDO;
    }

    /**
     *
     * @param node
     * @return
     */
    public static ItemDO getItemDO(Node node){
        //name="索引唯一标识" alias="ID" appField="" isDefault="1" flagValue=""
        String name = node.valueOf("@name");
        String alias = node.valueOf("@alias");
        String appField = node.valueOf("@appField");
        String isDefault = node.valueOf("@isDefault");
        String flagValue = node.valueOf("@flagValue");
        ItemDO itemDO = new ItemDO();
        itemDO.setName(name);
        itemDO.setAlias(alias);
        itemDO.setAppField(appField);
        itemDO.setIsDefault(isDefault);
        itemDO.setFlagValue(flagValue);
        return itemDO;
    }


}
